#include <functional>
#include "RingBuffer.hpp"
#include <unistd.h>
#include <pthread.h>

using namespace RingBufferModule;

void *Comsumer(void *args)
{
    RingBuffer<int> *ring_buffer = static_cast<RingBuffer<int> *>(args);
    while(true)
    {
        sleep(1);
        // 消费数据
        int data;
        ring_buffer->Pop(&data);
        
        // 处理数据：花时间
        std::cout << "消费了一个数据: " << data << std::endl;
    }
    
}

void *Productor(void *args)
{
    RingBuffer<int> *ring_buffer = static_cast<RingBuffer<int> *>(args);
    int data = 0;
    while(true)
    {
        // 获取数据: 花时间

        // 生产数据
        ring_buffer->Equeue(data);
        data++;
        std::cout << "生产了一个数据: " << data << std::endl;
    }
    
}

int main()
{
    RingBuffer<int> *ring_buffer = new RingBuffer<int>(5);

    pthread_t c1, c2, c3, p1, p2;
    pthread_create(&c1, nullptr, Comsumer, ring_buffer);
    pthread_create(&c2, nullptr, Comsumer, ring_buffer);
    pthread_create(&c3, nullptr, Comsumer, ring_buffer);
    pthread_create(&p1, nullptr, Productor, ring_buffer);
    pthread_create(&p2, nullptr, Productor, ring_buffer);

    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);
    pthread_join(c3, nullptr);
    pthread_join(p1, nullptr);
    pthread_join(p2, nullptr);

    delete ring_buffer;
    return 0;
}